package org.csu.canteen.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.canteen.entity.*;
import org.csu.canteen.persistence.*;
import org.csu.canteen.service.CatalogService;
import org.csu.canteen.vo.CategoryVO;
import org.csu.canteen.vo.IncludeMyListVO;
import org.csu.canteen.vo.ItemVO;
import org.csu.canteen.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service("catalogService")
public class CatalogServiceImpl implements CatalogService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private ItemQuantityMapper itemQuantityMapper;

    @Autowired
    private ProfileMapper profileMapper;

    @Autowired
    private LanguagesMapper languagesMapper;


    //已测试
    @Override
    public CategoryVO getCategory(String categoryId) {
        CategoryVO categoryVO=new CategoryVO();
        Category category=categoryMapper.selectById(categoryId);

        //条件查询
        QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
        //构造条件
        queryWrapper.eq("category",categoryId);
        List<Product> productList=productMapper.selectList(queryWrapper);

        //封装数据填入categoryVO
        categoryVO.setCategoryId(categoryId);
        categoryVO.setCategoryName(category.getName());
        categoryVO.setProductList(productList);
        return categoryVO;
    }

    //已测试
    @Override
    public ProductVO getProduct(String productId) {
        ProductVO productVO=new ProductVO();
        Product product=productMapper.selectById(productId);

        if (product == null) {
            return null; // 或者抛出自定义异常
        }
        Category category = categoryMapper.selectById(product.getCategoryId());

        //条件查询
        QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
        //构造条件
        queryWrapper.eq("productid",productId);
        List<Item> itemList=itemMapper.selectList(queryWrapper);

        //封装数据
        productVO.setProductId(productId);
        productVO.setCategoryId(product.getCategoryId());
        productVO.setProductName(product.getName());
        productVO.setItemList(itemList);

        /**/
        productVO.setCategoryName(category.getName());

        return productVO;
    }

    //已测试
    @Override
    public ItemVO getItem(String itemID) {
        ItemVO itemVO=new ItemVO();
        Item item=itemMapper.selectById(itemID);
        Product product=productMapper.selectById(item.getProductId());
        ItemQuantity itemQuantity=itemQuantityMapper.selectById(itemID);

        itemVO.setItemId(itemID);
        itemVO.setListPrice(item.getListPrice());
        itemVO.setProductName(product.getName());
        itemVO.setProductId(product.getProductId());
        itemVO.setAttribute1(item.getAttribute1());
        itemVO.setAttribute2(item.getAttribute2());
        itemVO.setAttribute3(item.getAttribute3());
        itemVO.setAttribute4(item.getAttribute4());
        itemVO.setAttribute5(item.getAttribute5());
        //itemVO.setDescription(product.getDescription());
        //处理描述
        String [] temp=product.getDescription().split("\"");
        itemVO.setDescriptionImage(temp[1]);
        itemVO.setDescriptionText(temp[2].substring(1));

        itemVO.setQuantity(itemQuantity.getQuantity());
        return itemVO;
    }

    //已测试
    @Override
    public IncludeMyListVO getIncludeMyList(String userId) {
        IncludeMyListVO includeMyListVO=new IncludeMyListVO();
        Profile profile=profileMapper.selectById(userId);
        //用户最爱的Category
        String favCategory=profile.getFavouriteCategoryId();
        //条件查询
        QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
        //构造条件
        queryWrapper.eq("category",favCategory);
        List<Product> productList=productMapper.selectList(queryWrapper);

        includeMyListVO.setProductList(productList);
        return includeMyListVO;
    }


    //已测试
    @Override
    public List<Product> searchProductList(String keyword) {
        // 条件查询
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        // 构造条件
        String query="%" + keyword.toLowerCase() + "%";
        queryWrapper.like("name", query);
        // 查询产品列表
        List<Product> productList = productMapper.selectList(queryWrapper);
        return productList;
    }

    //已测试
    @Override
    public boolean isItemInStock(String itemId) {
        ItemQuantity itemQuantity = itemQuantityMapper.selectById(itemId);
        return itemQuantity != null && itemQuantity.getQuantity() > 0;
    }

    @Override
    public List<Languages> getAllLanguages() {
        return languagesMapper.getAllLanguages();
    }

    @Override
    public List<ProductVO> getProductsByCategory(String categoryId) {
        return productMapper.findProductsByCategory(categoryId);
    }

    @Override
    public List<ProductVO> getAllProducts() {
        List<Product> productList = productMapper.selectList(null);
        return productList.stream()
                .map(product -> {
                    ProductVO productVO = new ProductVO(product);
                    // 手动设置其他字段
                    productVO.setCategoryName(categoryMapper.selectById(product.getCategoryId()).getName());
                    return productVO;
                })
                .collect(Collectors.toList());
    }

}
